home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP15 / POPFIND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.3 KB  |  115 lines

  1. /*--------------------------------------------------------
  2.    POPFIND.C -- Popup Editor Search and Replace Functions
  3.   --------------------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7. #include <string.h>
  8. #define MAX_STRING_LEN   256
  9.  
  10. static char szFindText [MAX_STRING_LEN] ;
  11. static char szReplText [MAX_STRING_LEN] ;
  12.  
  13. HWND PopFindFindDlg (HWND hwnd)
  14.      {
  15.      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
  16.  
  17.      fr.lStructSize      = sizeof (FINDREPLACE) ;
  18.      fr.hwndOwner        = hwnd ;
  19.      fr.hInstance        = NULL ;
  20.      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
  21.      fr.lpstrFindWhat    = szFindText ;
  22.      fr.lpstrReplaceWith = NULL ;
  23.      fr.wFindWhatLen     = sizeof (szFindText) ;
  24.      fr.wReplaceWithLen  = 0 ;
  25.      fr.lCustData        = 0 ;
  26.      fr.lpfnHook         = NULL ;
  27.      fr.lpTemplateName   = NULL ;
  28.  
  29.      return FindText (&fr) ;
  30.      }
  31.  
  32. HWND PopFindReplaceDlg (HWND hwnd)
  33.      {
  34.      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
  35.  
  36.      fr.lStructSize      = sizeof (FINDREPLACE) ;
  37.      fr.hwndOwner        = hwnd ;
  38.      fr.hInstance        = NULL ;
  39.      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
  40.      fr.lpstrFindWhat    = szFindText ;
  41.      fr.lpstrReplaceWith = szReplText ;
  42.      fr.wFindWhatLen     = sizeof (szFindText) ;
  43.      fr.wReplaceWithLen  = sizeof (szReplText) ;
  44.      fr.lCustData        = 0 ;
  45.      fr.lpfnHook         = NULL ;
  46.      fr.lpTemplateName   = NULL ;
  47.  
  48.      return ReplaceText (&fr) ;
  49.      }
  50.  
  51. BOOL PopFindFindText (HWND hwndEdit, int *piSearchOffset, LPFINDREPLACE pfr)
  52.      {
  53.      int   iLength, iPos ;
  54.      PSTR  pstrDoc, pstrPos ;
  55.  
  56.                // Read in the edit document
  57.  
  58.      iLength = GetWindowTextLength (hwndEdit) ;
  59.  
  60.      if (NULL == (pstrDoc = (PSTR) malloc (iLength + 1)))
  61.           return FALSE ;
  62.  
  63.      GetWindowText (hwndEdit, pstrDoc, iLength + 1) ;
  64.  
  65.                // Search the document for the find string
  66.  
  67.      pstrPos = strstr (pstrDoc + *piSearchOffset, pfr->lpstrFindWhat) ;
  68.      free (pstrDoc) ;
  69.  
  70.                // Return an error code if the string cannot be found
  71.  
  72.      if (pstrPos == NULL)
  73.           return FALSE ;
  74.  
  75.                // Find the position in the document and the new start offset
  76.  
  77.      iPos = pstrPos - pstrDoc ;
  78.      *piSearchOffset = iPos + strlen (pfr->lpstrFindWhat) ;
  79.  
  80.                // Select the found text
  81.  
  82.      SendMessage (hwndEdit, EM_SETSEL, iPos, *piSearchOffset) ;
  83.      SendMessage (hwndEdit, EM_SCROLLCARET, 0, 0) ;
  84.  
  85.      return TRUE ;
  86.      }
  87.  
  88. BOOL PopFindNextText (HWND hwndEdit, int *piSearchOffset)
  89.      {
  90.      FINDREPLACE fr ;
  91.  
  92.      fr.lpstrFindWhat = szFindText ;
  93.  
  94.      return PopFindFindText (hwndEdit, piSearchOffset, &fr) ;
  95.      }
  96.  
  97. BOOL PopFindReplaceText (HWND hwndEdit, int *piSearchOffset, LPFINDREPLACE pfr)
  98.      {
  99.                // Find the text
  100.  
  101.      if (!PopFindFindText (hwndEdit, piSearchOffset, pfr))
  102.           return FALSE ;
  103.  
  104.                // Replace it
  105.  
  106.      SendMessage (hwndEdit, EM_REPLACESEL, 0, (LPARAM) pfr->lpstrReplaceWith) ;
  107.  
  108.      return TRUE ;
  109.      }
  110.  
  111. BOOL PopFindValidFind (void)
  112.      {
  113.      return *szFindText != '\0' ;
  114.      }
  115.